<?php


function relo_workflow_email_list(){
  if (isset($_REQUEST['lang']) && !empty($_REQUEST['lang'])) {
    $language =  $_REQUEST['lang'];
  }else{
    $language = 'en';
  }

  $languagelist = locale_language_list(); //print_r($languagelist);die;
  
 
  $form['search']['language-inline'] = array(
    '#type' => 'select',
    '#title' => t('Language'),
    '#options' => $languagelist,
    '#default_value' => $language,
    '#attributes' => array('onchange' => 'window.location.href=\'?lang=\'+this.options[this.selectedIndex].value'),
  );

  $thepath = url('workflow_email_add');
  $form['actions']['add'] = array(
    '#type' => 'button',
    '#value' => t('Add') ,
    '#attributes' => array('onclick' => 'window.location.href=\''.$thepath.'\'; return false;'),
  );
  

  
  return $form;
}

function theme_relo_workflow_email_list($variables) {

  $output = '';
  $form = $variables['form']; 
  $output .= drupal_render_children($form);
  
  
  
  //-----------------------------------------------------------------------------
  if (isset($_REQUEST['lang']) && !empty($_REQUEST['lang'])) {
    $language =  $_REQUEST['lang'];
  }else{
    $language = 'en';
  }
  $query = db_select('email_template','t',array('fetch' => PDO::FETCH_ASSOC))
    ->fields('t');
  $query->condition('t.language',$language);
  $result = $query->execute();
  $rows = array();
  foreach ($result as $row) {
    if(isset($row['template_name'])){
      $row['template_name'] = array( 'data' => l($row['template_name'],'workflow_email/'.$row['id']) );
    }
    $row['Operations'] = array( 'data' => l(t('Edit'),'workflow_email/'.$row['id']) );
    
    unset($row['id']);
    $rows[] = $row;
  }
  //print_r($rows);
  
  
  $header = array();
  //$header[] = array('data' => t('ID'), 'width' => array('100'));
  $header[] = array('data' => t('Template Name'));
  $header[] = array('data' => t('Subject'));
  $header[] = array('data' => t('Body'));
  $header[] = array('data' => t('Language'));
  $header[] = array('data' => t('Operations'));

  $output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'email_template')));
  //-----------------------------------------------------------------------------

  
  
  
  //$search = drupal_render_children($form['search']);
  //$output = $search.$output;
  

  return $output;
}




function relo_workflow_email_add($form, $form_state){
  /*
  template_name
subject 
body
language
    */
    
   $form['template_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Template Name'),
    '#required' => TRUE,
    '#weight' => 0,
    '#maxlength' => 255,
  );
  $form['subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject'),
    '#required' => TRUE,
    '#weight' => 0.1,
    '#maxlength' => 255,
  );
  
  $languagelist = locale_language_list();
  $form['language'] = array(
    '#type' => 'select',
    '#title' => t('Language'),
    '#options' => $languagelist,
    '#weight' => 0.2,
  );
  
  
  $form['body'] = array(
    '#type' => 'textarea',
    '#title' => t('Template Content'),
    '#required' => TRUE,
    '#weight' => 0.3,
  );
  $form['template_tip'] = array(
      '#title' => t('Available template variables'),
      '#type' => 'item',
      '#description' => t('Available template variables in email template,e.g. &lt;employee_first_name&gt;, &lt;employee_last_name&gt;. More 
      please reference the form components form key.'),
      '#weight' => 0.4,
  );
  $form['actions']['#weight'] = 2;
  $form['actions']['save'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  $thepath = url('workflow_email');
  $form['actions']['reset'] = array(
    '#type' => 'button',
    '#value' => t('Reset'),
    '#attributes' => array('onclick' => 'window.location.href=\''.$thepath.'\'; return false;'),
  );
  return $form;
  
}

function relo_workflow_email_add_submit($form, &$form_state){
  $op = $form_state['values']['op'];
  $values = isset($form_state['values']) ? $form_state['values'] : array(); 
  
  if ($op == t('Save')) {
    $data = array();
    $data['template_name'] = $values['template_name'];
    $data['subject'] = $values['subject'];
    $data['body'] = $values['body'];
    $data['language'] = $values['language']; 
    //$data['id'] = null;  print_r($data);die;
    $query=  db_select('email_template','et')->fields('et');
    $query->condition('template_name',$values['template_name']);
    $result=$query->execute()->fetchAllAssoc('id', PDO::FETCH_ASSOC);
    if(count($result)>0){
       form_error($form['template_name'], t('Template Name already exist.'));
        return;
    }    
    $return = _create_workflow_email($data);
    $msg = t('Create workflow email success.');
    drupal_set_message($msg);      
    $form_state['redirect'] = 'workflow_email';
  } else {
    $path = 'workflow_email';
    drupal_goto($path);
  }
  
}





function relo_workflow_email_update($form, $form_state, $id){
  
  $query = db_select('email_template','t',array('fetch' => PDO::FETCH_ASSOC))
    ->fields('t');
  $query->condition('t.id',$id);
  $result = $query->execute()->fetch();


  $form['id'] = array(
    '#type' => 'value',
    '#value' => $result['id'],
  );
    
   $form['template_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Template Name'),
    '#required' => TRUE,
    '#weight' => 0,
    '#maxlength' => 255,
    '#default_value' => $result['template_name'],
  );
  $form['subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject'),
    '#required' => TRUE,
    '#weight' => 0.1,
    '#maxlength' => 255,
    '#default_value' => $result['subject'],
  );
  
  $languagelist = locale_language_list();
  $form['language'] = array(
    '#type' => 'select',
    '#title' => t('Language'),
    '#options' => $languagelist,
    '#weight' => 0.2,
    '#default_value' => $result['language'],
  );
  
  
  $form['body'] = array(
    '#type' => 'textarea',
    '#title' => t('Template Content'),
    '#required' => TRUE,
    '#weight' => 0.3,
    '#default_value' => $result['body'],
  );
  
 $form['template_tip'] = array(
    '#title' => t('Available template variables'),
    '#type' => 'item',
    '#description' => t('Available template variables in email template,e.g. &lt;employee_first_name&gt;, &lt;employee_last_name&gt;. More 
      please reference the form components form key.'),
      '#weight' => 0.4,
  );
  $form['actions']['#weight'] = 2;
  $form['actions']['save'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  $thepath = url('workflow_email');
  $form['actions']['reset'] = array(
    '#type' => 'button',
    '#value' => t('Reset'),
    '#attributes' => array('onclick' => 'window.location.href=\''.$thepath.'\'; return false;'),
  );
  
  return $form;
}

function relo_workflow_email_update_submit($form, &$form_state){
  $op = $form_state['values']['op'];
  $values = isset($form_state['values']) ? $form_state['values'] : array();
 
  
  
  if ($op == t('Save')) {
    $data = array();
    $data['id'] = $values['id'];
    $data['template_name'] = $values['template_name'];
    $data['subject'] = $values['subject'];
    $data['body'] = $values['body'];
    $data['language'] = $values['language']; 
    //$data['id'] = null;  print_r($data);die;
    $result = _update_workflow_email($data);
    $msg = t('Update workflow email success.');
    drupal_set_message($msg);

    $form_state['redirect'] = 'workflow_email';
  } else {
    $path = 'workflow_email';
    drupal_goto($path);
  }
  
}





function _create_workflow_email($data){
 
  $fields = array('template_name', 'subject', 'body', 'language');
  $query = db_insert('email_template')->fields($fields);
  $query->values($data);

  $query->execute();
}


function _update_workflow_email($data){
 
  $fields = array('template_name', 'subject', 'body', 'language');
  $query = db_update('email_template')
    ->fields(array(
      'template_name' => $data['template_name'],
      'subject' => $data['subject'],
      'body' => $data['body'],
      'language' => $data['language'],
    ))
    ->condition('id', $data['id']);
  $query->execute();
}

//------------------------------ page ------------------------------


//------------------------------ end page ------------------------------

